<?php

namespace app\api\controller\v1;
use app\common\library\Ems;
use app\common\library\Sms;
use app\common\controller\Api;
use app\common\logic\OrgLogic;
use app\common\model\org\Reply;
use app\common\validate\OrgReplyValidate;
use think\Db;
use fast\Http;

/**
 * 组织结构接口
 */
class Org extends Api
{
    protected $noNeedLogin = ['search', 'reply','about','history'];
    protected $noNeedRight = '*';


    /*搜索顶级单位*/
    public function search() {
        $search = input('search', '');
        $org = new OrgLogic();
        $list = $org->search($search);

        $this->success('搜索单位', $list);
    }

    public function checkmobile()
    {
        $mobile = input('mobile', '');
        $org = new OrgLogic();
        $res = $org->checkMobile($mobile);
        $this->success('手机号是否已存在', $res);
    }

    public function checkname()
    {
        $name = input('name', '');
        $org = new OrgLogic();
        $res = $org->checkName($name);
        $this->success('学校名称是否已存在', $res);
    }

    public function history()
    {
        $config = config('config')['epay']['wechat'];
        $code = $this->request->post("code");
        if (!$code) {
            $this->error("参数不正确");
        }
        $params = [
            'appid'      => $config['app_id'],
            'secret'     => $config['app_secret'],
            'js_code'    => $code,
            'grant_type' => 'authorization_code'
        ];
        $result = Http::sendRequest("https://api.weixin.qq.com/sns/jscode2session", $params, 'GET');
        if ($result['ret']){
            $json = (array)json_decode($result['msg'], true);
            if (isset($json['openid'])){
                $list = Db::name('org_cate_history')->alias('h')
                    ->join('edu_org_cate o','h.org_cate_id = o.id', 'left')
                    ->where('h.openid',$json['openid'])
                    ->group('h.org_cate_id')
                    ->order('h.logintime','desc')
                    ->limit(5)->field('o.id,o.name,o.type')->select();
                $this->success('历史单位', $list);
            }else{
                $this->error("获取openid失败");
            }
        }else{
            $this->error("微信授权失败");
        }

    }

    /*
     * 提交入住申请
     * */
    public function reply() {
        //数据验证
        (new OrgReplyValidate())->goCheck('reply');
        $mobile = input('mobile', '');
        $captcha = input('captcha', '');
        if (!Sms::check($mobile, $captcha, 'reply'))
        {
            $this->error('验证码错误');
        }
        $reply = new Reply($this->request->param());
        $name = $this->request->param('name');
        $res = (new OrgLogic())->checkName($name);
        if(!$res){
            $this->error('学校名称已存在!');
        }
        $res2 = (new OrgLogic())->checkMobile($mobile);
        if(!$res2){
            $this->error('手机号已存在!');
        }
        $res = $reply->allowField(true)->isUpdate(false)->save();
        if (!$res) {
            $this->error('申请失败');
        }
        $this->success('申请成功，请等待管理员审批');
    }
    /*获取关于我们/联系我们/用户须知/入驻协议/密码找回须知*/
    public function about() {
        $org_id = input('post.id', 0);
        $type = input('post.type',0);
        $data = Db::name('about')->where('org_cate_id','=',$org_id)->where('type','=',$type)->find();
        if($data){
            $this->success('', $data);
        }else{
            $this->error('暂无数据');
        }
    }


}
